home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / robotparser.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  7.6 KB  |  252 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. ''' robotparser.py
  5.  
  6.     Copyright (C) 2000  Bastian Kleineidam
  7.  
  8.     You can choose between two licenses when using this package:
  9.     1) GNU GPLv2
  10.     2) PSF license for Python 2.2
  11.  
  12.     The robots.txt Exclusion Protocol is implemented as specified in
  13.     http://info.webcrawler.com/mak/projects/robots/norobots-rfc.html
  14. '''
  15. import urlparse
  16. import urllib
  17. __all__ = [
  18.     'RobotFileParser']
  19.  
  20. class RobotFileParser:
  21.     ''' This class provides a set of methods to read, parse and answer
  22.     questions about a single robots.txt file.
  23.  
  24.     '''
  25.     
  26.     def __init__(self, url = ''):
  27.         self.entries = []
  28.         self.default_entry = None
  29.         self.disallow_all = False
  30.         self.allow_all = False
  31.         self.set_url(url)
  32.         self.last_checked = 0
  33.  
  34.     
  35.     def mtime(self):
  36.         '''Returns the time the robots.txt file was last fetched.
  37.  
  38.         This is useful for long-running web spiders that need to
  39.         check for new robots.txt files periodically.
  40.  
  41.         '''
  42.         return self.last_checked
  43.  
  44.     
  45.     def modified(self):
  46.         '''Sets the time the robots.txt file was last fetched to the
  47.         current time.
  48.  
  49.         '''
  50.         import time
  51.         self.last_checked = time.time()
  52.  
  53.     
  54.     def set_url(self, url):
  55.         '''Sets the URL referring to a robots.txt file.'''
  56.         self.url = url
  57.         (self.host, self.path) = urlparse.urlparse(url)[1:3]
  58.  
  59.     
  60.     def read(self):
  61.         '''Reads the robots.txt URL and feeds it to the parser.'''
  62.         opener = URLopener()
  63.         f = opener.open(self.url)
  64.         lines = [ line.strip() for line in f ]
  65.         f.close()
  66.         self.errcode = opener.errcode
  67.         if self.errcode in (401, 403):
  68.             self.disallow_all = True
  69.         elif self.errcode >= 400:
  70.             self.allow_all = True
  71.         elif self.errcode == 200 and lines:
  72.             self.parse(lines)
  73.         
  74.  
  75.     
  76.     def _add_entry(self, entry):
  77.         if '*' in entry.useragents:
  78.             self.default_entry = entry
  79.         else:
  80.             self.entries.append(entry)
  81.  
  82.     
  83.     def parse(self, lines):
  84.         '''parse the input lines from a robots.txt file.
  85.            We allow that a user-agent: line is not preceded by
  86.            one or more blank lines.'''
  87.         state = 0
  88.         linenumber = 0
  89.         entry = Entry()
  90.         for line in lines:
  91.             linenumber += 1
  92.             if not line:
  93.                 if state == 1:
  94.                     entry = Entry()
  95.                     state = 0
  96.                 elif state == 2:
  97.                     self._add_entry(entry)
  98.                     entry = Entry()
  99.                     state = 0
  100.                 
  101.             
  102.             i = line.find('#')
  103.             if i >= 0:
  104.                 line = line[:i]
  105.             
  106.             line = line.strip()
  107.             if not line:
  108.                 continue
  109.             
  110.             line = line.split(':', 1)
  111.             if len(line) == 2:
  112.                 line[0] = line[0].strip().lower()
  113.                 line[1] = urllib.unquote(line[1].strip())
  114.                 if line[0] == 'user-agent':
  115.                     if state == 2:
  116.                         self._add_entry(entry)
  117.                         entry = Entry()
  118.                     
  119.                     entry.useragents.append(line[1])
  120.                     state = 1
  121.                 elif line[0] == 'disallow':
  122.                     if state != 0:
  123.                         entry.rulelines.append(RuleLine(line[1], False))
  124.                         state = 2
  125.                     
  126.                 elif line[0] == 'allow':
  127.                     if state != 0:
  128.                         entry.rulelines.append(RuleLine(line[1], True))
  129.                         state = 2
  130.                     
  131.                 
  132.             line[0] == 'user-agent'
  133.         
  134.         if state == 2:
  135.             self.entries.append(entry)
  136.         
  137.  
  138.     
  139.     def can_fetch(self, useragent, url):
  140.         '''using the parsed robots.txt decide if useragent can fetch url'''
  141.         if self.disallow_all:
  142.             return False
  143.         if self.allow_all:
  144.             return True
  145.         if not urllib.quote(urlparse.urlparse(urllib.unquote(url))[2]):
  146.             pass
  147.         url = '/'
  148.         for entry in self.entries:
  149.             if entry.applies_to(useragent):
  150.                 return entry.allowance(url)
  151.         
  152.         if self.default_entry:
  153.             return self.default_entry.allowance(url)
  154.         return True
  155.  
  156.     
  157.     def __str__(self):
  158.         return []([ str(entry) + '\n' for entry in self.entries ])
  159.  
  160.  
  161.  
  162. class RuleLine:
  163.     '''A rule line is a single "Allow:" (allowance==True) or "Disallow:"
  164.        (allowance==False) followed by a path.'''
  165.     
  166.     def __init__(self, path, allowance):
  167.         if path == '' and not allowance:
  168.             allowance = True
  169.         
  170.         self.path = urllib.quote(path)
  171.         self.allowance = allowance
  172.  
  173.     
  174.     def applies_to(self, filename):
  175.         if not self.path == '*':
  176.             pass
  177.         return filename.startswith(self.path)
  178.  
  179.     
  180.     def __str__(self):
  181.         if not self.allowance or 'Allow':
  182.             pass
  183.         return 'Disallow' + ': ' + self.path
  184.  
  185.  
  186.  
  187. class Entry:
  188.     '''An entry has one or more user-agents and zero or more rulelines'''
  189.     
  190.     def __init__(self):
  191.         self.useragents = []
  192.         self.rulelines = []
  193.  
  194.     
  195.     def __str__(self):
  196.         ret = []
  197.         for agent in self.useragents:
  198.             ret.extend([
  199.                 'User-agent: ',
  200.                 agent,
  201.                 '\n'])
  202.         
  203.         for line in self.rulelines:
  204.             ret.extend([
  205.                 str(line),
  206.                 '\n'])
  207.         
  208.         return ''.join(ret)
  209.  
  210.     
  211.     def applies_to(self, useragent):
  212.         '''check if this entry applies to the specified agent'''
  213.         useragent = useragent.split('/')[0].lower()
  214.         for agent in self.useragents:
  215.             if agent == '*':
  216.                 return True
  217.             agent = agent.lower()
  218.             if agent in useragent:
  219.                 return True
  220.         
  221.         return False
  222.  
  223.     
  224.     def allowance(self, filename):
  225.         '''Preconditions:
  226.         - our agent applies to this entry
  227.         - filename is URL decoded'''
  228.         for line in self.rulelines:
  229.             if line.applies_to(filename):
  230.                 return line.allowance
  231.         
  232.         return True
  233.  
  234.  
  235.  
  236. class URLopener(urllib.FancyURLopener):
  237.     
  238.     def __init__(self, *args):
  239.         urllib.FancyURLopener.__init__(self, *args)
  240.         self.errcode = 200
  241.  
  242.     
  243.     def prompt_user_passwd(self, host, realm):
  244.         return (None, None)
  245.  
  246.     
  247.     def http_error_default(self, url, fp, errcode, errmsg, headers):
  248.         self.errcode = errcode
  249.         return urllib.FancyURLopener.http_error_default(self, url, fp, errcode, errmsg, headers)
  250.  
  251.  
  252.